<?php
namespace app\controllers;

use app\models\ResourceApp;
use app\models\ResourcePack;
use Yii;

use app\services\PackService;

class PackController extends AuthController
{
    public $currentHeader = 'header-resource';
    public $currentNav = 'pack-list-nav';
    public $pageTitle = '';

    public function init()
    {
        parent::init();
        $this->service = new PackService();
    }

    public function actionPackList()
    {
        $this->filter = 'true';
        $this->pageTile = $this->t('pack', 'Pack List');
        return $this->show();
    }

    public function actionManage()
    {
        $this->filter = 'true';
        $this->currentHeader = 'header-operation';
        $this->currentNav = 'pack-manage-nav';
        $this->pageTile = $this->t('pack', 'Pack Management');
        return $this->show('manageList');
    }

    public function actionEditPack()
    {
        $this->filter = 'true';
        $this->currentHeader = 'header-operation';
        $this->currentNav = 'pack-manage-nav';
        $this->pageTile = $this->t('pack', 'Edit Pack');
        $packId = $this->I('id');
        $pack = ResourcePack::find()->where(['id' => $packId])->asArray()->one();
        $this->pack = $pack;
        $apps = $this->service->getBindApp($packId);
        $this->apps = $apps;
        $appIds = array_column($apps, 'id');
        list($exts, $size) = $this->service->getAdspaceSizeAndExtsByAppIds($appIds);
        $this->exts = $exts;
        $this->size = $size;
        return $this->show();
    }

    public function actionNew()
    {
        $this->filter = 'true';
        $this->currentHeader = 'header-operation';
        $this->currentNav = 'pack-manage-nav';
        $this->pageTile = $this->t('pack', 'New Pack');
        return $this->show('newPack');
    }

    public function actionSavePack()
    {
        $pack = $this->I();
        unset($pack['app_ids']);
        $pack = $this->service->savePack($pack);
        $appIds = $this->I('app_ids');
        if ($pack) {
            $packId = $pack['id'];
            if ($this->service->savePackApp($packId, $appIds)) {
                $saved = $this->t('pack', 'Pack saved!');
                $this->ok($saved, 'manage');
            }
        }
        $saved = $this->t('pack', 'Pack save failed!');
        $this->ng($saved);
    }

    public function actionGetUnbindApp()
    {
        $type = $this->I('type');
        $key = $this->I('search');
        $items = $this->service->getUnbindApp($type, $key);
        $this->jsonReturn(['items' => $items]);
    }

    public function actionGetSizeListOnApps()
    {
        $apps = $this->I('app');
        list($exts, $size) = $this->service->getAdspaceSizeAndExtsByAppIds($apps);
        $this->ok(['sizeList' => $size, 'formatList' => $exts]);
    }

    public function actionEdit()
    {
        $this->filter = 'true';
        $this->currentHeader = 'header-operation';
        $this->currentNav = 'pack-manage-nav';
        $this->pageTile = $this->t('pack', 'Edit Pack');
        return $this->show('editPack');
    }

    public function actionGetPackList()
    {
        $packs = $this->service->getPackList();
        return $this->jsonReturn(['data' => $packs]);
    }

    public function actionGetPackApps()
    {
        $packId = $this->I('id');
        $apps = $this->service->getPackApps($packId);
        return $this->jsonReturn($apps);
    }

    public function actionMockup()
    {
        $this->service->addMockApps();
    }
}// end
